package com.example.mybatisplus.web.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.FavouriteMovieService;
import com.example.mybatisplus.model.domain.FavouriteMovie;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author fmx
 * @since 2024-11-09
 * @version v1.0
 */
@RestController
@RequestMapping("/api/favouriteMovie")
public class FavouriteMovieController {

    private final Logger logger = LoggerFactory.getLogger( FavouriteMovieController.class );

    @Autowired
    private FavouriteMovieService favouriteMovieService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        FavouriteMovie  favouriteMovie =  favouriteMovieService.getById(id);
        return JsonResponse.success(favouriteMovie);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        favouriteMovieService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateFavouriteMovie(FavouriteMovie  favouriteMovie) throws Exception {
        favouriteMovieService.updateById(favouriteMovie);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建FavouriteMovie
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(FavouriteMovie  favouriteMovie) throws Exception {
        favouriteMovieService.save(favouriteMovie);
        return JsonResponse.success(null);
    }

    /**
     * 描述:根据用户id查询储存的电影信息
     *
     */
    @GetMapping("/{userId}")
    public List<FavouriteMovie> getFavouriteMoviesByUserId(@PathVariable Long userId) {
        return favouriteMovieService.getFavouriteMoviesByUserId(userId);
    }

    @DeleteMapping("/{mvId}")
    public String deleteByMvId(@PathVariable Long mvId) {
        int result = favouriteMovieService.deleteByMvId(mvId);
        if (result > 0) {
            return "Movie deleted successfully";
        } else {
            return "Movie not found";
        }
    }

    // 添加保存喜爱电影的接口
    // 应该POST除mv_id外的所有信息（即表格中的mv_id,mv_name,mv_type,mv_information,user_id）
    @PostMapping("saveFavouriteMovie")
    public ResponseEntity<String> saveFavouriteMovie(@RequestBody FavouriteMovie favouriteMovie) {
        favouriteMovieService.saveFavouriteMovie(favouriteMovie);
        // 返回响应
        return ResponseEntity.status(HttpStatus.CREATED).body("Favourite movie saved successfully");
    }

    // 前端js文件应该写export const isFavourited = obj => http.POST('/api/favouriteMovie/isFavourited', obj)
    // obj要有name和user_id
    @PostMapping("isFavourited")
    public Boolean isFavourited(@RequestBody FavouriteMovie favouriteMovie) {
        // 返回值表示是否已收藏
        return favouriteMovieService.isFavourited(favouriteMovie);
    }
}

